TOOL.Category		= "Conveyor"
TOOL.Name			= "#Conveyor"
TOOL.Command		= nil
TOOL.ConfigName		= ""
TOOL.buffer = {}
TOOL.ents = {}
TOOL.count = 0

local directions = {
	{ "North", "0"},
	{ "South", "1"},
	{ "East", "2"},
	{ "West", "3"}
	}

if ( CLIENT ) then
	TOOL.ClientConVar[ "rate"  ] = "100"
	TOOL.ClientConVar[ "dist"  ] = "40"
	TOOL.ClientConVar[ "lp"  ] = "0"
	TOOL.ClientConVar[ "launch"  ] = "0"
	language.Add( "fungus", "fungus" )
	language.Add( "Tool_tool_conveyor_name", "Conveyor Tool" )
	language.Add( "Tool_tool_conveyor_desc", "Start conveyor track." )
	language.Add( "Tool_tool_conveyor_0", "Left click to start a conveyor track" )
	language.Add( "Tool_tool_conveyor_1", "Left click to add more points" )
	language.Add( "Tool_tool_conveyor_2", "Left click to add more points, right click to spawn controller" )
	language.Add( "Undone_tool_conveyor", "Undone Fungus" )
end

function TOOL:Reload()
if (self.count > 0) then
self.ents[self.count]:Remove()
table.remove(self.buffer)
table.remove(self.ents)
self.count = self.count - 1
			if (self.count < 1) then
			self:SetStage(0)
			elseif (self.count >= 1) and (self.count < 3) then
			self:SetStage(1)
			elseif (self.count >= 3) then
			self:SetStage(2)
			end
end
return true
end

function TOOL:LeftClick( trace )
			if (!trace.HitNonWorld) then
		 local pos = trace.HitPos + trace.HitNormal * tonumber(self:GetClientInfo("dist"))
            local conveyor = ents.Create( "conventpad" )
            conveyor:SetPos( pos )
			conveyor:SetAngles(Angle(90,0,0))
            conveyor:Spawn()
			conveyor:GetPhysicsObject():EnableMotion(false )
            table.insert(self.ents, conveyor)
			local t = {}
			t.pos = pos
			t.pos.z = t.pos.z + 1
			t.lp = tonumber(self:GetClientInfo("lp"))
			t.dr = tonumber(self:GetClientInfo("launch"))
			t.speed = tonumber(self:GetClientInfo("rate"))
			table.insert(self.buffer, t)
			self.count = self.count + 1
			conveyor.current = self.count
			conveyor.pos = pos
			conveyor.lp = tonumber(self:GetClientInfo("lp"))
			conveyor.dr = tonumber(self:GetClientInfo("launch"))
			conveyor.speed = tonumber(self:GetClientInfo("rate"))
			if (self.count < 1) then
			self:SetStage(0)
			elseif (self.count >= 1) and (self.count < 3) then
			self:SetStage(1)
			elseif (self.count >= 3) then
			self:SetStage(2)
			end
			end
			return true
end

function TOOL:RightClick(trace)
			if (self:GetStage() > 1) then
			if (!trace.HitNonWorld) then
			 local pos = trace.HitPos
            pos = pos + (trace.HitNormal * 40)
            local conveyor = ents.Create( "conventcontroller" )
            conveyor:SetPos( pos )
			conveyor:Spawn()
			conveyor.buffer = self.buffer
			conveyor.count = self.count
			conveyor.ents = self.ents
			for k,v in pairs(self.ents) do
			v.control = conveyor
			v.on = true
			local p = v:GetPhysicsObject()
			p:EnableMotion(true )
			p:EnableGravity( false )
			p:Wake()
			conveyor:DeleteOnRemove(v)
			end
			self.buffer = {}
			self.ents = {}
			self.count = 0
			self:SetStage(0)
			undo.Create("conveyor")
			undo.AddEntity( conveyor )
			undo.SetPlayer( self:GetOwner() )
			undo.Finish()
			end
			end
			return true
end

function TOOL:UpdateGhostThruster( ent, Player )

	if ( !ent ) then return end
	if ( !ent:IsValid() ) then return end

	local tr 	= utilx.GetPlayerTrace( Player, Player:GetCursorAimVector() )
	local trace 	= util.TraceLine( tr )
	if (!trace.Hit) then return end
		local pos = trace.HitPos + trace.HitNormal * tonumber(self:GetClientInfo("dist"))
		ent:SetPos( pos )
		ent:SetAngles(Angle(90,0,0))
	
end


function TOOL:Think()
	local m = "models/props_trainstation/trainstation_clock001.mdl"
	if (!self.GhostEntity || !self.GhostEntity:IsValid() || self.GhostEntity:GetModel() != self:GetClientInfo( "model" )) then
		self:MakeGhostEntity( m, Vector(0,0,0), Angle(0,0,0) )
	end
	
	self:UpdateGhostThruster( self.GhostEntity, self:GetOwner() )
	
end


function TOOL.BuildCPanel( clo )
	clo:AddControl( "Header", { Text = "#Conveyor", Description	= "#Conveyor Menu" } )
	
		
	clo:AddControl( 'Slider', { 
		Label = 'Rate units/sec to -arrive- at this position :',
		Type = "Integer",
		Min = 1,
		Max = 500,
		Command = 'tool_conveyor_rate'
	} )
	clo:AddControl( 'Slider', { 
		Label = 'Height/distance to spawn :',
		Type = "Integer",
		Min = 1,
		Max = 2000,
		Command = 'tool_conveyor_dist'
	} )
	clo:AddControl( 'Checkbox', { 
		Label = 'Unload point :',
		Description = "If checked, conveyors will unload here.",
		Command = 'tool_conveyor_lp'
	} )	
	
	
	combo = {}
	combo.Label = "Unload direction :"
	combo.MenuButton = 1
	combo.Folder = "settings/menu/main/construct/conveyor/"
	combo.Options = {}
	
	for k, v in pairs( directions ) do
	  combo.Options[ v[ 1 ] ] = { tool_conveyor_launch = v[ 2 ] }
	end

	clo:AddControl( "ComboBox", combo )
end


